<?php

class QshiftController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column3';
	public $sidebarMenu=array(
								array('label'=>'Roster Control', 'url'=>array('/roster/index')),
								array('label'=>'Shift Changes', 'url'=>array('/shiftChange/management')),
								array('label'=>'User Schedules', 'url'=>array('/schedule/index')),
								array('label'=>'Maintenance', 'url'=>array('/site/maintenance')),
								array('label'=>'Reporting', 'url'=>array('/site/reporting')),
							);

	
	protected function beforeAction($action) {
		if(!(Yii::app()->user->isGuest)) {
			$this->links =  Links::getTeamLinks(Yii::app()->user->team_id);
			
			if(empty($this->links)) {
				$this->links = array(array('label'=>'---','url'=>'#'));
				return true;
			} else
				return true;
		}
		else
			return true;
	}
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', 
				'actions'=>array('admin','delete','create','update','view'),
				'expression'=>'$user->can_create_edit_qshifts == 1',
			),
			array('allow', 
				'actions'=>array('index','my_Qshifts','team_Qshifts','ajaxTabContent','swap','doSwap'),
				'users'=>array('@'),
				'expression'=>'$user->can_create_qshift_change == 1',
			),			
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionTeamQShift($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Qshift;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Qshift']))
		{
			$model->attributes=$_POST['Qshift'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->idqshift));
		}

		$model->team_id = Yii::app()->user->team_id;
		
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Qshift']))
		{
			$model->attributes=$_POST['Qshift'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->idqshift));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$this->sidebarMenu=array(
								array('label'=>'Team Q-Shifts', 'url'=>array('/qshift/team_Qshifts' , 'team'=>Yii::app()->user->team_id)),
								array('label'=>'My Q-Shifts', 'url'=>array('/qshift/my_qshifts')),
								array('label'=>'Swap Q-Shift', 'url'=>array('/qshift/swap')),
								array('label'=>'Swap Management', 'url'=>array('/shiftchange/swap_management')),
								);		
		$this->render('index');
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Qshift('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Qshift']))
			$model->attributes=$_GET['Qshift'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Qshift the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Qshift::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Qshift $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='qshift-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function actionMy_Qshifts()
	{
		$this->sidebarMenu=array(
								array('label'=>'Team Q-Shifts', 'url'=>array('/qshift/team_Qshifts' , 'team'=>Yii::app()->user->team_id)),
								array('label'=>'My Q-Shifts', 'url'=>array('/qshift/my_qshifts')),
								array('label'=>'Swap Q-Shift', 'url'=>array('/qshift/swap')),
								array('label'=>'Swap Management', 'url'=>array('/shiftchange/swap_management')),
								);	
		$model=new Schedule('search');
		$model->unsetAttributes();  // clear any default values		
		$model->iduser = User::model()->findByAttributes(array('username'=>Yii::app()->user->id))->iduser;
		
		$this->render('my_qshifts', array( 'model'=>$model));
	}	
	
	public function actionTeam_Qshifts($team, $week=null, $year=null)
	{
		$this->sidebarMenu=array(
								array('label'=>'Team Q-Shifts', 'url'=>array('/qshift/team_Qshifts' , 'team'=>Yii::app()->user->team_id)),
								array('label'=>'My Q-Shifts', 'url'=>array('/qshift/my_qshifts')),
								array('label'=>'Swap Q-Shift', 'url'=>array('/qshift/swap')),
								array('label'=>'Swap Management', 'url'=>array('/shiftchange/swap_management')),
								);		
	
		///<<<<<<----------------DEFINE RELEVANT DATES------------------------------------------------------------->>>>
		
		if($week==null) {
			//echo "entered 1 with week = $week and year = $year";exit;
			$dateObject = new DateTime('NOW');
			$dateObject->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week  = $dateObject->format('W');
			$year = $dateObject->format('Y');		
			$week_start = new DateTime();
			$week_start->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week_start->setISODate($year,$week);
		} else {
			//echo "entered 2 with week = $week and year = $year";exit;
			$week_start = new DateTime();
			$week_start->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week_start->setISODate($year,$week);		
		}
		
		//creating an array with dayOfWeek => date entries
		$daysArray = array();
		$daysArray['monday'] = $week_start->format('Y-m-d');
		$daysArray['tuesday'] = date('Y-m-d', strToTime('next Tuesday '.$week_start->format('Y-m-d')));
		$daysArray['wednesday'] = date('Y-m-d', strToTime('next Wednesday '.$week_start->format('Y-m-d')));
		$daysArray['thursday'] = date('Y-m-d',  strToTime('next Thursday '.$week_start->format('Y-m-d')));
		$daysArray['friday'] = date('Y-m-d',  strToTime('next Friday '.$week_start->format('Y-m-d')));
		$daysArray['saturday'] = date('Y-m-d',  strToTime('next Saturday '.$week_start->format('Y-m-d')));
		$daysArray['sunday'] = date('Y-m-d',  strToTime('next Sunday '.$week_start->format('Y-m-d')));
		
		$this->render('team_qshifts', 
					  array(
							'team'=>$team,
							'week'=>$week,
							'year'=>$year,
							'days'=>$daysArray,
					));
	}
	
	public function actionAjaxTabContent($team, $date){
		
		$qshiftData = $this->getTeamQshiftsDate($team, $date);
		
		//print_r($qshiftData);
		$this->renderPartial('_tabContent',
							 array('qshiftData'=>$qshiftData,
							 'date'=>$date)
		);
	
	}
	
	public function getTeamQshiftsDate($team, $date,$userSearchCriteria=null) {
		
		// get list of enabled users 
		
		if($userSearchCriteria==null)
			$userSearchCriteria = array('condition'=>'enabled = 1 and team = '.$team);
		
		$users = User::model()->findAll($userSearchCriteria);
		
		/** MULTIDIMENSIONAL ARRAY STRUCTURE
	
			$rows	idqshift	name=value
								start_time
								end_time
								users	iduser	username=value
												fullname=value
												wasChanged=value
												isQshiftSwap=value
												changeId=value
							
										iduser	username=value
												fullname=value
												wasChanged=value
												isQshiftSwap=value
												changeId=value							
					idqshift	name=value	
								start_time
								end_time		
								users	iduser	username=value
												fullname=value
												wasChanged=value
												isQshiftSwap=value
												changeId=value
		**/
		$rows = array();
		
		///<<<<<<----------------ITERATE THROUGH USERS------------------------------------------------------------->>>>
		foreach ($users as $user) {
							
			///<<<<<<----------------GET USER SCHEDULE FOR DATE------------------------------------------------------------->>>>
			$userSchedule = Schedule::getUserScheduleForDate($user->iduser, $date);
			
			
			if($userSchedule!=null) {
					//echo 'user='.$user->username.' schedule='.$userSchedule->idschedule.' qshift='.$userSchedule->idqshift.'<br>';
				
				//check if key for this qshift exists in rows, if not, create it with all other details
				if( !(array_key_exists($userSchedule->idqshift, $rows)) ) {
					
					// echo "Created qshift key for $userSchedule->idqshift ! <br>";  //***TESTER
					$currentQshift = $userSchedule->qshift_relation;
					$rows[$userSchedule->idqshift] = array();
					$rows[$userSchedule->idqshift]['idqshift'] = $currentQshift->idqshift;
					$rows[$userSchedule->idqshift]['name'] = $currentQshift->name;
					if($currentQshift->availability) {
						$rows[$userSchedule->idqshift]['start_time'] = $currentQshift->start_time;
						$rows[$userSchedule->idqshift]['end_time'] = $currentQshift->end_time;
					} else {
						$rows[$userSchedule->idqshift]['start_time'] = " --:--:-- ";
						$rows[$userSchedule->idqshift]['end_time'] = " --:--:-- ";
					}
					$rows[$userSchedule->idqshift]['availability'] = $currentQshift->availability;
					$rows[$userSchedule->idqshift]['users'] = array();
					
				} //qshift key created			
				
				$rows[$userSchedule->idqshift]['users'][$user->iduser] = array();
				$rows[$userSchedule->idqshift]['users'][$user->iduser]['username']= $user->username;
				$rows[$userSchedule->idqshift]['users'][$user->iduser]['fullname']= $user->fullname;
				
				if($userSchedule->wasChanged) { 
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['wasChanged']= $userSchedule->wasChanged;
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['isQshiftSwap']= $userSchedule->isQshiftChange;
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['changeId']= $userSchedule->changeId;
				} else {
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['wasChanged']= $userSchedule->wasChanged;
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['isQshiftSwap']= null;
					$rows[$userSchedule->idqshift]['users'][$user->iduser]['changeId']= null;					
				}
			}
		
		} //ends forearch user

		//print_r($rows);exit;
		
		if($rows!=null) {
		
			// Obtain a list of columns
			foreach ($rows as $key => $row) {
				$availability[$key]  = $row['availability'];
				$startTime[$key]  = $row['start_time'];
			}
			
			
						//print_r($startTime);

			// Sort the data with start time descending
			// Add $parentMainArray as the last parameter, to sort by the common key
			array_multisort($availability, SORT_DESC, $startTime, SORT_ASC, $rows);			
		}
		//echo "before print rows <br>";
		//print_r($rows);exit;
		
		//return rows of multidimensional array				
		return $rows;			
	
	}	
	
	public function actionSwap($week=null,$year=null,$userId_A=null,$userId_B=null){

		$this->sidebarMenu=array(
								array('label'=>'Team Q-Shifts', 'url'=>array('/qshift/team_Qshifts' , 'team'=>Yii::app()->user->team_id)),
								array('label'=>'My Q-Shifts', 'url'=>array('/qshift/my_qshifts')),
								array('label'=>'Swap Q-Shift', 'url'=>array('/qshift/swap')),
								array('label'=>'Swap Management', 'url'=>array('/shiftchange/swap_management')),
								);		
		
		if($userId_A==null)			
			$userId_A = Yii::app()->user->iduser; //get invoking user's username
		
		///<<<<<<----------------DEFINE RELEVANT DATES------------------------------------------------------------->>>>
		
		if($week==null) {
			//echo "entered 1 with week = $week and year = $year";exit;
			$dateObject = new DateTime('NOW');
			$dateObject->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week = $dateObject->format('W');
			$year = $dateObject->format('Y');		
			$week_start = new DateTime();
			$week_start->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week_start->setISODate($year,$week);
		} else {
			//echo "entered 2 with week = $week and year = $year";exit;
			$week_start = new DateTime();
			$week_start->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$week_start->setISODate($year,$week);		
		}
		
		//creating an array with dayOfWeek => date entries
		$daysArray = array();
		$daysArray[$week_start->format('l jS')] = $week_start->format('Y-m-d');
		$daysArray[date('l jS', strToTime('next Tuesday '.$week_start->format('Y-m-d')))] = date('Y-m-d', strToTime('next Tuesday '.$week_start->format('Y-m-d')));
		$daysArray[date('l jS', strToTime('next Wednesday '.$week_start->format('Y-m-d')))] = date('Y-m-d', strToTime('next Wednesday '.$week_start->format('Y-m-d')));
		$daysArray[date('l jS', strToTime('next Thursday '.$week_start->format('Y-m-d')))] = date('Y-m-d',  strToTime('next Thursday '.$week_start->format('Y-m-d')));
		$daysArray[date('l jS', strToTime('next Friday '.$week_start->format('Y-m-d')))] = date('Y-m-d',  strToTime('next Friday '.$week_start->format('Y-m-d')));
		$daysArray[date('l jS', strToTime('next Saturday '.$week_start->format('Y-m-d')))] = date('Y-m-d',  strToTime('next Saturday '.$week_start->format('Y-m-d')));
		$daysArray[date('l jS', strToTime('next Sunday '.$week_start->format('Y-m-d')))] = date('Y-m-d',  strToTime('next Sunday '.$week_start->format('Y-m-d')));
		
		//print_r($_GET);
		
		if(isset($_GET['isAjaxRequest'])) {
			if(!($_GET['isAjaxRequest']=='1')){
			
			$this->render('swap',
						  array(
								'userA'=>$userId_A,
								'userB'=>$userId_B,
								'week'=>$week,
								'year'=>$year,
								'days'=>$daysArray,
						));
			} else {
				$this->renderPartial('_swapContent',
									  array(
											'userA'=>$userId_A,
											'userB'=>$userId_B,
											'week'=>$week,
											'year'=>$year,
											'days'=>$daysArray
											),
									false,
									true
							);			
			}
			
		} else {
		    //echo "else way";exit;
			$this->render('swap',
						  array(
								'userA'=>$userId_A,
								'userB'=>$userId_B,
								'week'=>$week,
								'year'=>$year,
								'days'=>$daysArray,
						));		
		}
	}
	
	public function checkSwapOptions($userA,$userB,$date){
	
		$scheduleModel_A = Schedule::model()->getUserScheduleForDate($userA,$date); 
		$scheduleModel_B = Schedule::model()->getUserScheduleForDate($userB,$date); 
		
		if(($scheduleModel_A==null) or ($scheduleModel_B==null)){ //checking for null values
			//echo "<br>matched 0";
			return array('result'=>false, 'message'=>'There was a problem loading the schedules for the selected users, please contact michamen@cisco.com');
		}elseif( $scheduleModel_A->idqshift == $scheduleModel_B->idqshift) { //checking if the qshifts are the same
			//echo "<br>matched 1";
			return array('result'=>false, 'message'=>'Same Qshift already');
		}elseif(!($this->checkShiftQshiftEnabledAllowed($scheduleModel_A->idshift,$scheduleModel_A->idqshift,$scheduleModel_B->idshift,$scheduleModel_B->idqshift))){
			//checking if both the qshifts and shifts are enabled and that the qshifts allow swaps
			//echo "<br>matched 2";
			return array('result'=>false, 'message'=>'A user has a Shift or Qshift that is not enabled or that doesn\'t allow SWAPs');			
		}elseif(!($this->checkSwapShiftHours($scheduleModel_A,$scheduleModel_B)) ){ //checking the hours of the qshifts are within the shift hours
			//echo "<br>matched 3";
			return array('result'=>false, 'message'=>'Users are on different Shifts. Please request a Shift Change instead');
		}elseif($this->checkSwapActiveChange($userA,$userB,$date)){ //checking if there are active changes by any of the users already
			//echo "<br>matched 4";
			return array('result'=>false, 'message'=>'You, or the target user, already have a change pending or approved for this day, and only one is allowed per user per day!');
		} else { // all checks passed!
			//echo "<br>matched 5";
			return array('result'=>true, 'message'=>'Click to process SWAP!');
		}
	}
	
	private function checkShiftQshiftEnabledAllowed($shiftA,$qshiftA,$shiftB,$qshiftB){
	
		//Yii::log('', CLogger::LEVEL_ERROR, 'Mike, entered checkShiftQshiftEnabledAllowed');
		//echo 'Mike, entered checkShiftQshiftEnabledAllowed <br> received values '.$shiftA.', '.$qshiftA .' ,'. $shiftB.' , '.$qshiftB.'<br>';
	
		$shiftModelA = Shift::model()->findByPk($shiftA);
		$qshiftModelA = QShift::model()->findByPk($qshiftA);
		$shiftModelB = Shift::model()->findByPk($shiftB);
		$qshiftModelB = QShift::model()->findByPk($qshiftB);	
		
		if( !($shiftModelA->enabled) or 
			!($qshiftModelA->enabled) or 
			!($shiftModelB->enabled) or 
			!($qshiftModelB->enabled) or
			!($qshiftModelB->enabled) or
			!($qshiftModelA->swap_allowed) or 
			!($qshiftModelB->swap_allowed)
		  ) {
			return false;
		} else {
			return true;
		}
	}
	
	private function checkSwapShiftHours($scheduleA,$scheduleB) {
	
		//Yii::log('', CLogger::LEVEL_ERROR, 'Mike, entered checkSwapShiftHours');
		//echo 'Mike, entered checkSwapShiftHours<br>';
	
		$shiftModelA = Shift::model()->findByPk($scheduleA->idshift);
		$qshiftModelA = QShift::model()->findByPk($scheduleA->idqshift);
		$shiftModelB = Shift::model()->findByPk($scheduleB->idshift);
		$qshiftModelB = QShift::model()->findByPk($scheduleB->idqshift);
		
		
		if( 
			( 
			  ( strtotime($qshiftModelB->start_time) >= strtotime($shiftModelA->start_time) )
				&& 
			  ( strtotime($qshiftModelB->end_time) <= strtotime($shiftModelA->end_time) )
				&& 
			  ( strtotime($qshiftModelA->start_time) >= strtotime($shiftModelB->start_time) )
				&& 
			  ( strtotime($qshiftModelA->end_time) <= strtotime($shiftModelB->end_time) )
			    
			)
			
			&&
			
			($shiftModelA->idshift == $shiftModelB->idshift)
		  ) 
		{
			return true;
		} else 			
			return false;			
	}		
	
	private function checkSwapActiveChange($userA, $userB, $date) {
	
		//echo 'Mike, entered checkSwapActiveChange<br>';
	
		//Yii::log('', CLogger::LEVEL_ERROR, 'Mike, entered checkSwapActiveChange');
		// start active shift change check
		$criteria=new CDbCriteria;
		$criteria->condition = "day_requested like '".$date."' \n
								and (request_status <> ". ShiftChange::STATUS_CANCELLED ." and request_status <> ". ShiftChange::STATUS_DENIED.") \n
								and (requesting_user = '".$userA."' or target_user = '".$userA."'
									or requesting_user = '".$userB."' or target_user = '".$userB."')";			
		
		$shiftChangeResult = ShiftChange::model()->findAll($criteria);		
		
		// checking if user already has shift change pending or approved for this day
		if ($shiftChangeResult!=null)	
			return true;
		else
			return false;
	}
	
	public function actionDoSwap($userA,$userB,$date){
		
		//getting schedules for each user
		$userAScheduleModel = Schedule::model()->getUserScheduleForDate($userA,$date); 
		$userBScheduleModel = Schedule::model()->getUserScheduleForDate($userB,$date); 
		
		//populate new shift change
		$model=new ShiftChange;
		$model->requesting_user = $userA;
		$model->current_shift = $userAScheduleModel->idshift;
		$model->requested_shift = $userBScheduleModel->idshift;
		$model->requested_qshift = $userBScheduleModel->idqshift;
		$model->day_requested = $date;
		$model->request_status = ShiftChange::STATUS_APPROVED;
		$model->target_user = $userB;
		$model->comments = 'Processed by '.Yii::app()->user->id;
		$model->team_id = Yii::app()->user->team_id;
		$model->is_swap = 1;
		
		$result = $this->checkSwapOptions($userA,$userB,$date);
		
		if($result['result']) {
			//save model
			if($model->save(false)){

				echo "<center><h2> Saved Successfully!!! Check it here:&nbsp";
				
				$imghtml = CHtml::image(Yii::app()->theme->baseUrl.'/images/magnifier.png', 
									  'View Swap',
									  array(
											'style'=>'width:25px;height:25px;display:inline;'
											)
									  );
				
				echo CHtml::link($imghtml, 
								 array('shiftChange/viewSwap', 'id'=>$model->getPrimaryKey()), 
								 array('title'=>'Click to view Swap details',
									   'style'=>'display:inline;',
									   'target' => '_blank'
										)
								);
				
				echo "</h2></center><br>";	 
				
				//send email notification to requestor
				$this->sendSwapNotification($model->requesting_user,$model->request_status,$model->idshift_change);
				
				//send email notification to target_user if present
				$this->sendSwapNotification($model->target_user,$model->request_status,$model->idshift_change);				
				
			} else {
				echo "<center><font color=red>Error while saving because: <br> ".$model->getErrors()."<br><br><font></center>";
			}		
		} else {
			
			echo "<center><font color=red>Error while saving because: <br> ".$result['message']."</font><br><br>
				  <font color=blue><b>Please refresh the page to get the current swaps available!<br><br></b><font></center>";	
		}
	}
	
public function sendSwapNotification($toUser,$status,$shiftChange)
	{
		
		//BYPASS EMAIL SENDING HERE
		return true;
		
		$userModel = User::model()->findByPk($toUser);
		
		if($userModel == null) {
			return false;
		}
		
		$newStatus = ShiftChange::getStatus($status);
		
		$content = "<font color=blue><b><h3> Q-Shift SWAP Status Notification </h3></b></font>
					<font color=black><b> Hello ".$userModel->fullname.", <br><br> The status of one of your opened Q-Shift SWAPs has changed to <span style='color:red;font-weight:bold;'>"
					.$newStatus.
					"</span></b>
					<br><br>
					To see it, click "
					. CHtml::link(CHtml::encode('here'), array('http://10.198.28.163/staffer/index.php/shiftChange', 'id'=>$shiftChange)) .					
					"</font><br>
					<h5> Please don't reply to this email... cuz no one will read it :-P</h5>
					";
								
		// Plain text content
		$plainTextContent = "STAFFer Q-Shift SWAP Status Notification \n
							 \n
							 Hello ".$userModel->fullname.", \n
							 The status of one of your Shift Change requests has changed to \"".$newStatus."\"\n
							 \n
							 Please visit the STAFFer tool and go to \"Q-Shifts/Swap Management\" for more details \n
							 \n
							 Please don't reply to this email... cuz no one will read it :-P";
		
		// Get mailer
		$SM = Yii::app()->swiftMailer;								
		
		// Get config
		$mailHost = 'smtp.gmail.com';
		$mailPort = 465; // Optional

		// New transport
		$Transport = $SM->smtpTransport($mailHost, $mailPort);
	 
		// Mailer
		$Mailer = $SM->mailer($Transport);
	 
		// New message
		$Message = $SM
			->newMessage('STAFFer Notification')
			->setFrom(array('cr.tac.staffer@gmail.com' => 'CR-TAC STAFFer'))
			->setTo(array($userModel->email => $userModel->fullname))	
			->addPart($content, 'text/html')
			->setBody($plainTextContent);
	 
		// Send mail
		$result = $Mailer->send($Message);		
		
		return $result;
		
		//$this->render('index');
	}		
	
	
}
